#include "wrinifile.h"
#include<QSettings>
#include<QtDebug>
void WriteIniFiles()//写配置文件
{
    //直接使用QSettings类来读写INI文件
    QSettings *ConfigWriteINIFiles=new QSettings("MyFiles.ini",QSettings::IniFormat);

    ConfigWriteINIFiles->setValue("/database/ip","192.168.12.189");
    ConfigWriteINIFiles->setValue("/database/port","3308");
    ConfigWriteINIFiles->setValue("/database/user","root");
    ConfigWriteINIFiles->setValue("/database/password","123456");

    ConfigWriteINIFiles->setValue("/notice/version","5.6");
    ConfigWriteINIFiles->setValue("/notice/datetime","2022-10-25 16:27:23");

    //写完之后删除指针
    delete ConfigWriteINIFiles;
}
void ReadIniFiles()//读配置文件
{
    QSettings *ConfigReadINIFiles=new QSettings("MyFiles.ini",QSettings::IniFormat);

    QString strip=ConfigReadINIFiles->value("/database/ip").toString();
    QString strport=ConfigReadINIFiles->value("/database/port").toString();
    QString struser=ConfigReadINIFiles->value("/database/user").toString();
    QString strpassword=ConfigReadINIFiles->value("/database/password").toString();

    QString strversion=ConfigReadINIFiles->value("/notice/version").toString();
    QString strdatetime=ConfigReadINIFiles->value("/notice/datetime").toString();

    //输出读取配置文件的参数信息
    qDebug()<<"读取INI配置文件参数选项如下：";
    qDebug()<<"MySQL数据库IP地址:"<<strip.toUtf8().data();
    qDebug()<<"数据库端口:"<<strport.toUtf8().data();
    qDebug()<<"数据库用户:"<<struser.toUtf8().data();
    qDebug()<<"数据库密码:"<<strpassword.toUtf8().data();
    qDebug()<<"数据库版本:"<<strversion.toUtf8().data();
    qDebug()<<"数据库日期:"<<strdatetime.toUtf8().data();

    //将读取配置文件完成后，删除指针
    delete ConfigReadINIFiles;

}
void ReadIniFilesIsKey()//都全部的
{
    QSettings setting("MyFiles.ini",QSettings::IniFormat);

    foreach(QString key,setting.allKeys())
    {
        qDebug()<<key.toUtf8().data()<<setting.value(key).toString().toUtf8().data();
    }
}
